package com.bjsxt.commom.dao.impl;

import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import com.bjsxt.commom.Util.PageInfoUtil;
import com.bjsxt.commom.dao.IBasicDao;

public abstract class BasicDaoImpl<T> extends HibernateDaoSupport implements IBasicDao<T>
{
	@Override
	public int update(T t)
	{
		try
		{
			this.getHibernateTemplate().update(t);
			return 1;
		} catch (Exception e)
		{
		}
		return 0;
	}

	@Override
	public int delete(T t)
	{
		try
		{
			this.getHibernateTemplate().delete(t);
			return 1;
		} catch (Exception e)
		{
		}
		return 0;
	}
	/*
	 * 保存一个对象
	 * */
	@Override
	public int insert(T t)
	{
		return (int) this.getHibernateTemplate().save(t);
	}
	
	/*
	 * 抽象方法要求继承的子类重写，为自己的findone写条件
	 * */
	@Override
	public abstract T findOne(String type,Map<String,Object> sourceMap);
	@Override
	public abstract List<T> findList(PageInfoUtil pageInfoUtil,String type,Map<String,Object> condMap);
	
	protected List<T> findListDao(String hql,Map<String, Object> condMap)
	{
		return (List<T>) this.getHibernateTemplate().execute(new HibernateCallback<List<T>>()
		{

			@Override
			public List<T> doInHibernate(Session session) throws HibernateException
			{
				Query query = session.createQuery(hql);
				query.setProperties(condMap);
				return (List<T>)query.getResultList();
			}
		});
	}
	
	protected List<T> findPageListDao(PageInfoUtil pageInfoUtil,String hql,Map<String, Object> condMap)
	{
		return (List<T>) this.getHibernateTemplate().execute(new HibernateCallback<List<T>>()
		{
			@Override
			public List<T> doInHibernate(Session session) throws HibernateException
			{
				/* 开始分页查询 */
				/* 查询总记录数 */
				String sqlCount = "select count(*) ";
				String start = "from";
				String end = "order by";
				sqlCount += hql.substring(hql.indexOf(start), hql.indexOf(end));
				Query query = session.createQuery(sqlCount);
				query.setProperties(condMap);
				Long count = (Long) query.getSingleResult();
				pageInfoUtil.setTotalRecord(count.intValue());
				/* 开始查询数据 */
				query = session.createQuery(hql);
				query.setProperties(condMap);
				/* 设置分页信息 */
				query.setFirstResult(pageInfoUtil.getCurrRecord());
				query.setMaxResults(pageInfoUtil.getPageSize());
				return (List<T>)query.getResultList();
			}
		});
	}
	
	/*
	 * 查询一条记录
	 * */
	protected T findOneDao(String hql,Map<String, Object> condMap)
	{
		return this.getHibernateTemplate().execute(new HibernateCallback<T>()
		{
			
			@Override
			public T doInHibernate(Session session) throws HibernateException
			{
				/* 
				 * session = connection  一个链接
				 * 
				 * 获得一个query对象 == preparedStatement 
				 * */
				try
				{
					Query<T> query = session.createQuery(hql);
					/* 设置参数 */
					query.setProperties(condMap);
					return query.getSingleResult();
				} catch (Exception e)
				{
					return null;
				}
			}
		});
	}
	
}
